﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MMS.BLL;
using MMS.Model;

namespace MarketMS
{
    public partial class BuyGoods : Form
    {
        public BuyGoods()
        {
            InitializeComponent();
        }

        private void BuyGoods_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2) //判断是否按得是F2
            {
                //使文本框获取焦点
                txtGoodsCode.Focus();
            }
            else if (e.KeyCode == Keys.F5)
            {
                JieSuan js = new JieSuan();
                js.BuyList = buyList;//设置要结算的商品集合
                js.ShowDialog();
            }
        }

        private GoodsManager gm = new GoodsManager();


        private List<BuyGoodsInfo> buyList = new List<BuyGoodsInfo>();

        private void btnSearch_Click(object sender, EventArgs e)
        {

            if (txtGoodsCode.Text != "")
            {
                //开始搜索商品信息
                List<GoodsInfo> list = gm.GetListByCode(txtGoodsCode.Text);//查询数据

                BuyGoodsInfo item = new BuyGoodsInfo();
                if (list.Count == 0)
                {
                    MessageBox.Show("商品编号输入错误");
                }
                else if (list.Count == 1)
                {//只有一个直接显示
                    item.GoodsCode = list[0].GoodsCode;
                    item.GoodsName = list[0].GoodsName;
                    item.SalePrice = list[0].SalePrice;
                    item.BuyCount = 1;
                    buyList.Add(item);
                }
                else
                {//多个的情况
                    SelectGoods sg = new SelectGoods(null);
                    sg.showDataList = list;//设置要显示数据
                    sg.ShowDialog();//调用显示方法
                    GoodsInfo g = sg.SelectedGoods;//获取选中的商品数据(对象的属性值)
                    if (g != null)
                    {
                        item.GoodsCode = g.GoodsCode;
                        item.GoodsName = g.GoodsName;
                        item.SalePrice = g.SalePrice;
                        item.BuyCount = 1;
                        buyList.Add(item);
                    }
                }
                RefreshBuyList();

                txtGoodsCode.Text = "";
            }
        }

        /// <summary>
        /// 刷新购买列表
        /// </summary>
        private void RefreshBuyList()
        {
            //dgvGoodsList.DataSource = buyList;//绑定数据
            //new BindingList<集合的类型>(集合的名称);
            dgvGoodsList.DataSource = new BindingList<BuyGoodsInfo>(buyList);
            //开始统计购买数据
            int totalCount = 0;
            float totalMoney = 0.0f;
            foreach (BuyGoodsInfo item in buyList)
            {
                totalCount += item.BuyCount;//累加购买数量
                totalMoney += (float)item.XiaoJi;//累加小计
            }
            lblTotalMoney.Text = string.Format("{0:C}", totalMoney);
            lblTotalCount.Text = totalCount.ToString();
        }

        private void txtGoodsCode_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) //判断是否按得是回车键
            {
                if (txtGoodsCode.Text != "")
                {
                    //使文本框获取焦点
                    btnSearch_Click(null, null);
                }
            }
        }

        private void BuyGoods_Load(object sender, EventArgs e)
        {
            //禁止DataGridView自动创建咧
            dgvGoodsList.AutoGenerateColumns = false;
        }

        private void btnJiesuan_Click(object sender, EventArgs e)
        {
            JieSuan js = new JieSuan();
            js.BuyList = buyList;//设置要显示的数据
            js.ShowDialog();
            if (js.IsScuccess)
            {
                buyList.Clear();//清空已购买的商品集合
            }
            RefreshBuyList();//完成结算 刷新列表
        }

        private void dgvGoodsList_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                RefreshBuyList();
            }
        }
    }
}
